/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PesquisaAssociado.java
 *
 * Created on 20/09/2009, 15:29:05
 */
package convenios.telas;

import conexao.DAOException;
import convenios.bd.ConvenioDAO;
import convenios.classes.ConsultaConvenioTableModel;
import convenios.classes.Convenio;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

public class FrmPesquisaConvenio extends javax.swing.JFrame {

    /** Creates new form PesquisaAssociado */
    public FrmPesquisaConvenio() {
        try {
            dao = new ConvenioDAO();
            List<Convenio> cl = dao.getConvenios();
            model = new ConsultaConvenioTableModel(cl);
        } catch (DAOException ex) {
            //passando a mensagem capturada na excessão criada daoexception
            JOptionPane.showMessageDialog(null, ex.getMessage());

            model = new ConsultaConvenioTableModel(new ArrayList<Convenio>());
        }

        initComponents();
        setVisible(true);
        tableConvenios.setModel(model);
        //comando para capturar ações de mouse e teclado
        tableConvenios.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        TableColumn coluna = tableConvenios.getColumnModel().getColumn(0);

        coluna.setPreferredWidth(50);
        coluna = tableConvenios.getColumnModel().getColumn(1);
        coluna.setPreferredWidth(200);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ConveniosjPanel = new javax.swing.JPanel();
        PesquisadeConveniadosjTextField = new javax.swing.JTextField();
        ScrollPane = new javax.swing.JScrollPane();
        tableConvenios = new javax.swing.JTable();
        nomeConveniadoojLabel = new javax.swing.JLabel();
        BarradeTarefasjToolBar = new javax.swing.JToolBar();
        AdicionarjButton = new javax.swing.JButton();
        editarConvenio = new javax.swing.JButton();
        excluirConvenio = new javax.swing.JButton();
        PesquisarjButton = new javax.swing.JButton();
        SeparadordeTarefasjSeparator = new javax.swing.JToolBar.Separator();
        ImprimirjButton = new javax.swing.JButton();
        VisualizarjButton = new javax.swing.JButton();
        RelatoriojButton = new javax.swing.JButton();
        VoltarjButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Empresa Conveniada");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);
        setLocationByPlatform(true);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        ConveniosjPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        ConveniosjPanel.setPreferredSize(new java.awt.Dimension(800, 600));
        ConveniosjPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        ConveniosjPanel.add(PesquisadeConveniadosjTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 490, -1));

        tableConvenios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Razão Social", "Nome do Responsável", "Telefone Fixo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableConvenios.setToolTipText("Tabela de Associados");
        tableConvenios.setAutoscrolls(false);
        tableConvenios.getColumnModel().getColumn(0).setPreferredWidth(400);
        tableConvenios.getColumnModel().getColumn(1).setPreferredWidth(100);
        ScrollPane.setViewportView(tableConvenios);
        tableConvenios.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableConvenios.getColumnModel().getColumn(1).setPreferredWidth(200);
        tableConvenios.getColumnModel().getColumn(2).setPreferredWidth(30);

        ConveniosjPanel.add(ScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 810, 380));

        nomeConveniadoojLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        nomeConveniadoojLabel.setText("Razão Social");
        ConveniosjPanel.add(nomeConveniadoojLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 23, -1, -1));

        BarradeTarefasjToolBar.setFloatable(false);
        BarradeTarefasjToolBar.setRollover(true);

        AdicionarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/novo.png"))); // NOI18N
        AdicionarjButton.setText("Adicionar");
        AdicionarjButton.setToolTipText("Adicionar Associado");
        AdicionarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AdicionarjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        AdicionarjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        AdicionarjButton.setName(""); // NOI18N
        AdicionarjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        AdicionarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AdicionarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdicionarConvenio(evt);
            }
        });
        BarradeTarefasjToolBar.add(AdicionarjButton);

        editarConvenio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/alterar.png"))); // NOI18N
        editarConvenio.setText("Editar");
        editarConvenio.setToolTipText("Editar Associado");
        editarConvenio.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editarConvenio.setMaximumSize(new java.awt.Dimension(80, 90));
        editarConvenio.setMinimumSize(new java.awt.Dimension(80, 90));
        editarConvenio.setPreferredSize(new java.awt.Dimension(80, 90));
        editarConvenio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editarConvenio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditarConvenio(evt);
            }
        });
        BarradeTarefasjToolBar.add(editarConvenio);

        excluirConvenio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/excluir.png"))); // NOI18N
        excluirConvenio.setText("Excluir");
        excluirConvenio.setToolTipText("Excluir Associado");
        excluirConvenio.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        excluirConvenio.setMaximumSize(new java.awt.Dimension(80, 90));
        excluirConvenio.setMinimumSize(new java.awt.Dimension(80, 90));
        excluirConvenio.setPreferredSize(new java.awt.Dimension(80, 90));
        excluirConvenio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        excluirConvenio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExcluirConvenio(evt);
            }
        });
        BarradeTarefasjToolBar.add(excluirConvenio);

        PesquisarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/pesquisar.png"))); // NOI18N
        PesquisarjButton.setText("Pesquisar");
        PesquisarjButton.setToolTipText("Pesquisar Associado");
        PesquisarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        PesquisarjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        PesquisarjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        PesquisarjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        PesquisarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        BarradeTarefasjToolBar.add(PesquisarjButton);
        BarradeTarefasjToolBar.add(SeparadordeTarefasjSeparator);

        ImprimirjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/impressao.png"))); // NOI18N
        ImprimirjButton.setText("Imprimir");
        ImprimirjButton.setToolTipText("Imprimir");
        ImprimirjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        ImprimirjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        ImprimirjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        ImprimirjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        ImprimirjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        BarradeTarefasjToolBar.add(ImprimirjButton);

        VisualizarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/visualizar.png"))); // NOI18N
        VisualizarjButton.setText("Visualizar");
        VisualizarjButton.setToolTipText("Visualizar Cadastro");
        VisualizarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        VisualizarjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        VisualizarjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        VisualizarjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        VisualizarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        BarradeTarefasjToolBar.add(VisualizarjButton);

        RelatoriojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/relatorio.png"))); // NOI18N
        RelatoriojButton.setText("Relatórios");
        RelatoriojButton.setToolTipText("Relatórios");
        RelatoriojButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        RelatoriojButton.setMaximumSize(new java.awt.Dimension(80, 90));
        RelatoriojButton.setMinimumSize(new java.awt.Dimension(80, 90));
        RelatoriojButton.setPreferredSize(new java.awt.Dimension(80, 90));
        RelatoriojButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        BarradeTarefasjToolBar.add(RelatoriojButton);

        VoltarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/voltar.png"))); // NOI18N
        VoltarjButton.setText("Voltar");
        VoltarjButton.setToolTipText("Voltar");
        VoltarjButton.setFocusable(false);
        VoltarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        VoltarjButton.setMaximumSize(new java.awt.Dimension(80, 90));
        VoltarjButton.setMinimumSize(new java.awt.Dimension(80, 90));
        VoltarjButton.setPreferredSize(new java.awt.Dimension(80, 90));
        VoltarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        VoltarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voltarPrincipal(evt);
            }
        });
        BarradeTarefasjToolBar.add(VoltarjButton);

        ConveniosjPanel.add(BarradeTarefasjToolBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 470, 720, 80));

        getContentPane().add(ConveniosjPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 860, 560));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmPesquisaConvenio().setVisible(true);
            }
        });
    }
    private void AdicionarConvenio(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdicionarConvenio
        new FrmCadastroConvenio(new Convenio());

    }//GEN-LAST:event_AdicionarConvenio

    private void ExcluirConvenio(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExcluirConvenio
        int linha = tableConvenios.getSelectedRow();
        if (linha >= 0) {
            Convenio cl = model.listaConvenio.get(linha);
            System.out.println("Codigo " + cl.getIdConvenio());
            new FrmCadastroConvenio(cl);
        } else {
            JOptionPane.showConfirmDialog(this, "Selecione uma pessoa primeiro");
        }


    }//GEN-LAST:event_ExcluirConvenio

    private void EditarConvenio(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditarConvenio
        int linha = tableConvenios.getSelectedRow();
        if (linha >= 0) {
            Convenio cl = model.listaConvenio.get(linha);
            System.out.println("Codigo " + cl.getIdConvenio());
            new FrmCadastroConvenio(cl);
        } else {
            JOptionPane.showConfirmDialog(this, "Selecione uma pessoa primeiro");
        }
    }//GEN-LAST:event_EditarConvenio

    private void voltarPrincipal(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voltarPrincipal
        this.dispose();
    }//GEN-LAST:event_voltarPrincipal
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AdicionarjButton;
    private javax.swing.JToolBar BarradeTarefasjToolBar;
    private javax.swing.JPanel ConveniosjPanel;
    private javax.swing.JButton ImprimirjButton;
    private javax.swing.JTextField PesquisadeConveniadosjTextField;
    private javax.swing.JButton PesquisarjButton;
    private javax.swing.JButton RelatoriojButton;
    private javax.swing.JScrollPane ScrollPane;
    private javax.swing.JToolBar.Separator SeparadordeTarefasjSeparator;
    private javax.swing.JButton VisualizarjButton;
    private javax.swing.JButton VoltarjButton;
    private javax.swing.JButton editarConvenio;
    private javax.swing.JButton excluirConvenio;
    private javax.swing.JLabel nomeConveniadoojLabel;
    private javax.swing.JTable tableConvenios;
    // End of variables declaration//GEN-END:variables
    private ConsultaConvenioTableModel model;
    private ConvenioDAO dao;
}
